package com.ss.task.test;

import com.ss.task.model.MyTaskEntity;
import com.ss.task.model.TaskEntity;
import com.ss.task.model.UserEntity;
import com.ss.task.service.TaskService;
import com.ss.task.util.BaseController;
import com.ss.webutil.struct.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tan Zhaowei
 * 个人发布的任务情况
 */
@RestController
@RequestMapping("test/myTask/")
public class MyTaskController extends BaseController {
    @Autowired
    TaskService taskService;

    @RequestMapping("doing")
    public ResultModel<List<MyTaskEntity>> MyDoing(Integer offset, Integer length, HttpSession session){
        UserEntity user = getUser(session);
        ResultModel<List<TaskEntity>> resultModel = taskService.getAllPubTasksByPubUser(user.getId(), offset, length);
        return giveResultofMyTask(resultModel);
    }

    @RequestMapping("done")
    public ResultModel<List<MyTaskEntity>> MyDone(Integer offset, Integer length, HttpSession session){
        UserEntity user = getUser(session);
        ResultModel<List<TaskEntity>> resultModel = taskService.getAllFinishedTasksByPubUser(user.getId(), offset, length);
        return giveResultofMyTask(resultModel);
    }

    @RequestMapping("over")
    public ResultModel<List<MyTaskEntity>> MyOver(Integer offset, Integer length, HttpSession session){
        UserEntity user = getUser(session);
        ResultModel<List<TaskEntity>> resultModel = taskService.getAllEvaluateTasksByPubUser(user.getId(), offset, length);
        return giveResultofMyTask(resultModel);
    }

    //转换格式
    public ResultModel<List<MyTaskEntity>> giveResultofMyTask(ResultModel<List<TaskEntity>> resultModel){
        List<TaskEntity> list = resultModel.getData();
        List<MyTaskEntity>  listMyTask = new ArrayList<MyTaskEntity>();

        for(int i=0;i<list.size();i++) {
            MyTaskEntity myTaskEntity = new MyTaskEntity();
            myTaskEntity.setName(list.get(i).getTitle());
            myTaskEntity.setType(list.get(i).getType().getTitle());
            myTaskEntity.setPublisher(list.get(i).getLeader().getRealname());
            myTaskEntity.setStart_time(list.get(i).getStartDate());
            myTaskEntity.setEnd_time(list.get(i).getEndDate());

            listMyTask.add(myTaskEntity);
        }

        ResultModel<List<MyTaskEntity>> resultModel1 = new ResultModel<List<MyTaskEntity>>(listMyTask);

        return resultModel1;
    }
}
